<?php
/**
 * User: Administrator
 * Note: 初始化
 */
namespace Admin\Controller;

use Think\Controller;

class BaseController extends Controller
{
    public $_uid;
    public $_member;

    public function _initialize()
    {
        $_uid = session('admin_uid');
        if (!$_uid) {
            if (IS_AJAX) {
                appReturn(0, '请先登陆', U('Public/login'));
            } else {
                redirect(U('Public/login'));
            }
        }
        $_member = D('Member')->getInfoById($_uid);
        if (!$_member) {
            if (IS_AJAX) {
                appReturn(0, '账户不存在或被禁用', U('Public/login'));
            } else {
                $this->error('账户不存在或被禁用');
            }
        }
        $this->_uid    = $_uid;
        $this->_member = $_member;
        $this->assign('_uid', $_uid);
        $this->assign('_member', $_member);
        // 权限判断
        $_auth = D('Auth')->getInfoByName(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME);
        if (!$_auth) {
            if (IS_AJAX) {
                appReturn(0, '无权访问');
            } else {
                $this->error('无权访问');
            }
        }
        $_have_auth = D('Auth')->checkGroupById($_auth['id'], $_member['auth_group_id']);
        if (!$_have_auth) {
            if (IS_AJAX) {
                appReturn(0, '无权访问');
            } else {
                $this->error('无权访问');
            }
        }
        // 获取该用户快捷菜单
        $quick_menus = D('Menu')->getQuickMenus($_uid);
        $quick_menus = $quick_menus?:array();
        $this->assign('quick_menus', $quick_menus);
        // 获取当前用户组菜单
        $_menus = D('Menu')->getMenus($_member['auth_group_id']);
        $this->assign('_menus', $_menus);
        // 获取当前菜单信息
        $_menu = D('Menu')->order('sort asc')->getInfoByUrl(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME);
        $this->assign('_menu', $_menu);

        // 获取配置
        $config     = array();
        $configList = M('config')->select();
        foreach ($configList as $val) {
            $config[$val['name']] = $val['value'];
        }
        //print_r($config);die();
        $this->assign('configs', $config);
    }
}
